package com.example.mall.service.impl;

import com.example.mall.mapper.SpManageMapper;
import com.example.mall.mapper.UserMapper;
import com.example.mall.pojo.*;
import com.example.mall.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.List;

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
    SpManageMapper manageMapper;
    UserMapper userMapper;

//    查询全部用户列表并分页
    @Override
    public OnePage getUserList(String query, Integer pagenum, Integer pagesize) {
        if(query == "") {
            PageHelper.startPage(pagenum, pagesize);
            List<UserOut> users1 = userMapper.selectAll();
            PageInfo<UserOut> info = new PageInfo<>(users1, pagesize);
            OnePage page = new OnePage();
            page.setUsers(info.getList());
            page.setPagenum(info.getPageNum());
            page.setTotal((int) info.getTotal());

            return page;
        }else {
            PageHelper.startPage(pagenum, pagesize);
            List<UserOut> users1 = userMapper.selectAllByQuery(query);
            PageInfo<UserOut> info = new PageInfo<>(users1, pagesize);
            OnePage page = new OnePage();
            page.setUsers(info.getList());
            page.setPagenum(info.getPageNum());
            page.setTotal((int) info.getTotal());

            return page;
        }
    }


//    根据id删除用户
    @Override
    public Boolean deleteUser(Integer id) {
        int i = userMapper.deleteUser(id);
        if(i==1){
            return true;
        }
        return  false;
    }

//    根据id查询用户
    @Override
    public UserById getUserInfoById(Integer userId) {
        UserById spManager = userMapper.selectById(userId);
        if(spManager==null){
            return null;
        }
        return spManager;
    }

//    插入用户
    @Override
    public Boolean insertUser(UserOut userOut) {
        int i = userMapper.insertUser(userOut);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateStateById(Boolean state, Integer id) {
        int updateState = userMapper.updateStateById(state,id);
        if(updateState==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateInfo(String mg_email, String mg_mobile, Integer mg_id) {
        int updateInfo = userMapper.updateInfo(mg_email,mg_mobile,mg_id);
        if(updateInfo==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateRoles(Integer rid, Integer id) {
        int i = userMapper.updateRoles(rid, id);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

//    @Override
//    public Boolean updateRole(Integer id) {
////        int updateRole = userMapper.updateRole(id);
////        if(updateRole==1){
////            return true;
////        }
//        return false;
//    }

//    @Override
//    public List<RoleList> selectRole(){
//        List<RoleList> roleLists = userMapper.selectRole();
//        if(roleLists==null){
//            return null;
//        }
//        return roleLists;
//    }
//

}
